#include "epoll_server.hpp"
#include "sock.hpp"
#include<string>
#include<cstdlib>
#include"app_interface.hpp"
#include "util.hpp"
const int back_log = 5;
const int timeout = -1;


void Usage(std::string proc)
{

  std::cout <<"Usage: " << "\n\t" <<proc <<" port" << std::endl;
}



int main(int argc , char* argv[])
{
  if(argc !=2 ){
    Usage(argv[0]);
    exit(5);
  }
  //这个是与服务器listen_sock相关的
  uint16_t port = atoi(argv[1]);
  int listen_sock = ns_sock::Sock::Socket();
  ns_comm::SetNonBlock(listen_sock);
  ns_sock::Sock::Bind(listen_sock,port);
  ns_sock::Sock::Listen(listen_sock,back_log);
 

  //这个是Epoller事件管理器
  ns_epoll::Epoller epoller;
  epoller.initEpoller();

  ns_epoll::EventItem item;
  item.sock = listen_sock;
  item.R = &epoller;

  //listen只需要关心读事件
  //accepter函数
  item.ManagerCallBack(ns_appinterface::accepter,nullptr,nullptr);

  //将listen_sock托管给epoller!
  epoller.addEven(listen_sock,EPOLLIN|EPOLLET,item);
  
  while(true){
    epoller.loopOnce(timeout);

  }

  return 0;
}
